package org.dromara.commission.listener.redisPlus;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellExtra;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.excel.core.ExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 系统用户自定义导入
 *
 * @author 小将lcz
 */
@Slf4j
public abstract  class RedisPlusImportListener<T extends RedisPlusEntity> extends AnalysisEventListener<T> implements ExcelListener<T> {

    private boolean CLEAR = false;//是否先清空再导入（false:不清空， ture:清空）
    private int readNum = 0;//当前读取数
    private boolean redisPushEnd = false;//当前读取数
    protected HashMap<String,List<T>> listHashMap =new HashMap<>();
    private RedisIndexTable redisIndexTable = RedisPlusUtil.initIndexTable(this.getClass());//根据子类的名来初始化索引表

    private StringBuilder analysisMsg = new StringBuilder();

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        super.onException(exception, context);
    }

    @Override
    public void invoke(T data, AnalysisContext context) {
        readNum++;
        invokeBefore(data);//单步执行过程前的操作
        if(data.ACCESS){
            String key = key(data);
            if(listHashMap.containsKey(key)) listHashMap.get(key).add(data);
            else{
                List<T> list = new ArrayList<>();
                list.add(data);
                listHashMap.put(key,list);
            }
        }
        invokeAfter(data);//单步执行过程后的操作
    }

    /***
    * @description 生成key
    */
    public abstract String key(T data);

    /***
     * @description 单步执行过程前的操作
     */
    public abstract void invokeBefore(T data);

    /***
     * @description 单步执行过程后的操作
     */
    public abstract void invokeAfter(T data);

    /***
     * @description 清空原来的redis数据
     */
    public void clear(){
        CLEAR = true;
        RedisPlusUtil.clear(redisIndexTable);
        redisIndexTable = new RedisIndexTable(redisIndexTable.getHeadStr());
    }

    /***
    * @description 获取分析结果msg
    */
    public String getAnalysis() {
        return analysisMsg.toString();
    }

    /***
    * @description 获取读取出的数据集
    */
    public HashMap<String, List<T>> getResultData() {
        return listHashMap;
    }

    /***
     * @description 获取已经读取的行数
     */
    public int getReadNum() {
        return readNum;
    }

    /***
     * @description 获取redis上传完毕标记
     */
    public boolean getRedisPushEnd() {
        return redisPushEnd;
    }

    @Override
    public void extra(CellExtra extra, AnalysisContext context) {
        super.extra(extra, context);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        analysisMsg = RedisPlusUtil.push(listHashMap,redisIndexTable);
        redisPushEnd = true;//更新上传完毕的标记
    }

    @Override
    public boolean hasNext(AnalysisContext context) {
        return super.hasNext(context);
    }

    @Override
    public ExcelResult<T> getExcelResult() {
        return new ExcelResult<T>() {

            @Override
            public String getAnalysis() {
                return analysisMsg.toString();
            }

            @Override
            public List<T> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }

        };
    }
}
